package top.fengleifeng.question;

/**
 * @program: leetcode-test
 * @description:
 * @author: fengleifeng
 * @create: 2020-10-09 16:01
 **/
public class Num814二叉树剪枝 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    /**
     * 思路：递归解决
     */
    class Solution {
        public TreeNode pruneTree(TreeNode root) {
            return aa(root) ? root : null;
        }

        private boolean aa(TreeNode now) {
            if(null==now){
                return false;
            }
            boolean b1 = aa(now.left);
            boolean b2 = aa(now.right);
            if(!b1){
                now.left = null;
            }
            if(!b2){
                now.right = null;
            }
            return now.val==1||b1||b2;
        }
    }
}
